using Microsoft.Practices.EnterpriseLibrary.Logging;

using TfsSpam.Utilities;

namespace TfsSpam.Differ.Logging
{
    /// <summary>
    /// The SpamLogger class makes sure to add the id of the changeset
    /// being processed to all logged messages.
    /// </summary>
    internal class SpamLogger : TfsLogger
    {
        #region -- Class constants
        /// <summary>
        /// The logging context entry for the <see cref="Changeset"/> id.
        /// </summary>
        private const string ChangesetContext = "Changeset";
        #endregion

        #region -- Private class members
        /// <summary>
        /// The id of the changeset the logger is associated with.
        /// </summary>
        private int _changesetId;
        #endregion

        #region -- Constructors
        /// <summary>
        /// Standard constructor for the class, sets the changeset id
        /// so this can be included in all logged messages.
        /// </summary>
        /// <param name="changesetId">The id of a changeset being processed.</param>
        public SpamLogger( int changesetId )
        {
            _changesetId = changesetId;
        }
        #endregion

        #region -- Base class overrides
        /// <summary>
        /// Overridden to add the id of the changeset being
        /// processed to the context for all log messages.
        /// </summary>
        protected override void AddContext()
        {
            // Let the base class do its work first...
            base.AddContext();

            // ...then add our changeset id.
            if ( _changesetId != 0 )
            {
                Logger.SetContextItem( ChangesetContext, _changesetId );
            }
        }
        #endregion
    }
}
